﻿using UnityEngine;

namespace JH
{
    /// <summary>
    ///  图表组件的基接口，所有组件的接口都应该由此派生
    /// </summary>
    public interface IChartBase
    {
    }

    public static class ChartBaseExtension
    {
        /// <summary>
        ///  获取当前图表组件的图表
        /// </summary>
        public static IChart<TPoint> GetChart<TPoint>(this IChartBase chartBase) where TPoint : struct
        {
            if (chartBase == null)
            {
                Debug.LogWarning("当前图表组件为空，无法获取图表");
                return null;
            }

            var comp = chartBase as Component;
            if (!comp)
            {
                Debug.LogWarning("当前图表组件不是Component类型，无法获取图表");
                return null;
            }

            return comp.GetComponentInParent<IChart<TPoint>>(true);
        }


        /// <summary>
        ///  获取当前图表组件的类型
        /// </summary>
        public static TFind GetChartComp<TFind, TPoint>(this IChartBase chartBase, bool includeInactive = false)
            where TFind : IChartBase
            where TPoint : struct
        {
            var chart = chartBase.GetChart<TPoint>();
            if (chart == null)
            {
                Debug.LogWarning("当前图表组件没有所属的图表，无法获取组件");
                return default;
            }

            // 如果chartBase本身就是T类型，直接返回
            if (chart is TFind tChart) return tChart;

            // 如果chartBase是Component类型，获取其子组件
            if (chart is not Component comp)
            {
                Debug.LogWarning($"当前图表组件不是Component类型，无法获取类型为{typeof(TFind).Name}的组件");
                return default;
            }

            return comp.GetComponentInChildren<TFind>(includeInactive);
        }

        /// <summary>
        ///  获取当前图表组件的所有类型
        /// </summary>
        public static TFind[] GetChartComps<TFind, TPoint>(this IChartBase chartBase, bool includeInactive = false)
            where TFind : IChartBase
            where TPoint : struct
        {
            var chart = chartBase.GetChart<TPoint>();
            if (chart == null)
            {
                Debug.LogWarning("当前图表组件没有所属的图表，无法获取组件");
                return null;
            }

            // 如果chartBase是Component类型，获取其子组件
            if (chart is not Component comp)
            {
                Debug.LogWarning($"当前图表组件不是Component类型，无法获取类型为{typeof(TFind).Name}的组件");
                return null;
            }

            return comp.GetComponentsInChildren<TFind>(includeInactive);
        }

        /// <summary>
        ///  设置Render的RectTransform强制为和父物体一起缩放一样大
        /// </summary>
        public static void SetRectTransform(this IChartBase comp)
        {
            if (comp == null)
            {
                Debug.LogWarning("当前图表组件为空，无法设置RectTransform");
                return;
            }

            if (comp is not Component component)
            {
                Debug.LogWarning("当前图表组件不是Component类型，无法设置RectTransform");
                return;
            }

            var rect = component.GetComponent<RectTransform>();
            if (!rect)
            {
                Debug.LogWarning("当前图表组件没有RectTransform组件，无法设置RectTransform");
                return;
            }

            rect.localRotation = Quaternion.identity;
            rect.localPosition = Vector3.zero;
            rect.localScale = Vector3.one;
            rect.anchorMin = rect.anchoredPosition = rect.sizeDelta = rect.pivot = Vector2.zero;
            rect.anchorMax = Vector2.one;
        }
    }
}